<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Common\Server;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Contract\TranslatorInterface;
use Hyperf\Context\ApplicationContext;
use Hyperf\HttpServer\Contract\RequestInterface;
use App\Common\Demand\Server\LoginLog as LogInterfaces;
use App\Common\Demand\Models\LoginLog as LogModel;
use App\Common\Demand\Server\Jwt;
use App\Common\Demand\Server\Utils;
use App\Common\Exception\Store\{LoginException,DataNotFound};
class LoginLog implements LogInterfaces
{
    #[Inject]
    protected  LogModel $model;
    #[Inject]
    protected  Jwt $jwt;
    #[Inject]
    protected  Utils $utils;
    #[Inject]
    protected  TranslatorInterface $translatorInterface;
    public function getListPage(array $requestData, array $field=['*']):array{
        return $this->model->getListPage($requestData,$field);
    }
    public function getList(array $where, array $field=['*']):array{
        return $this->model->getList($where,$field);
    }
     //登录成功
    public function loginSuccess(int $targetId,int $targetType):string{
        $data=[
            "target_id"=>$targetId,
            "target_type"=>$targetId,
            "error_number"=>$this->model->getErrorNumber($targetId,$targetType),
            "login_ip"=>$this->getRealIp(),
            "status"=>2
        ];
        $this->model->saveAdd($data);
        $this->model->updateLogStatus($targetId,$targetType);
        return $this->jwt->createToken($data);
     }
     //登录失败
     public function loginError(int $targetId,int $targetType):bool{
        $data=[
            "target_id"=>$targetId,
            "target_type"=>$targetId,
            "error_number"=>$this->model->getErrorNumber($targetId,$targetType),
            "login_ip"=>$this->getRealIp(),
            "status"=>1
        ];
        return (bool)$this->model->saveAdd($data);
     }
     //检查登录锁定状态
     public function checkLog(int $targetId,int $targetType):bool{
        $errorNumber=$this->model->getErrorNumber($targetId,$targetType);
        $lastTime=$this->model->getLastTime($targetId,$targetType);
        if($errorNumber>2&&time()-$lastTime<($errorNumber*600)){
            return false;
        }else{
            return true;
        }
     }
     //解除锁定状态
     public function Unlock(int $targetId,int $targetType):bool{
       return (bool)$this->model->updateLogStatus($targetId,$targetType);
     }
     public function getRealIp(): string
    {
        $request = ApplicationContext::getContainer()->get(RequestInterface::class);
        $headers = $request->getHeaders();
        if(isset($headers['x-forwarded-for'][0]) && !empty($headers['x-forwarded-for'][0])) {
            return $headers['x-forwarded-for'][0];
        } elseif (isset($headers['x-real-ip'][0]) && !empty($headers['x-real-ip'][0])) {
            return $headers['x-real-ip'][0];
        }
        $serverParams = $request->getServerParams();
        return $serverParams['remote_addr'] ?? '';
    }
}